package com.hdoit.ares.web.tp.zzb.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.ares.logical.tp.zzb.model.ZzbDataCallback;
import com.hdoit.ares.logical.tp.zzb.service.IZzbDataCallbackService;
import com.hdoit.ares.logical.viewmodel.tp.zzp.ZzbDataCallbackSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;

/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年11月27日 15:39:39
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/zzb/car/")
public class ZzbDataCallbackController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/zzb/car/";
    }

    private IZzbDataCallbackService zzbDataCallbackService;
    public IZzbDataCallbackService getZzbDataCallbackService() {
        return zzbDataCallbackService;
    }
    @Resource
    public void setZzbDataCallbackService(IZzbDataCallbackService zzbDataCallbackService) {
        this.zzbDataCallbackService = zzbDataCallbackService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="zzbDataCallbackDetailed";
    private final String saveViewName="save";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,ZzbDataCallbackSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(zzbDataCallbackService.queryCount(search));
        List<ZzbDataCallback> zzbDataCallbacks= zzbDataCallbackService.queryList(search,pagerModel);
        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("zzbDataCallbacks", zzbDataCallbacks);
        model.addAttribute("zzbDataCallbackSearch", search);
        if(ajaxPost){
            return view(outputViewName);
        }
        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String zzbDataCallbackDetailed(Model model,String key){
        ZzbDataCallback zzbDataCallback=new ZzbDataCallback();
        if(!StringUtils.isEmpty(key)){
            zzbDataCallback=zzbDataCallbackService.queryModel(key);
        }
        model.addAttribute("zzbDataCallback", zzbDataCallback);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,ZzbDataCallback zzbDataCallback){
        ActionResult actionResult;
        if(zzbDataCallbackService.applyChange(zzbDataCallback)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }
        model.addAttribute("zzbDataCallback", zzbDataCallback);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);
        return view(detailedViewName);
    }
}
